package com.awei.controller;


import com.awei.domain.Result;
import com.awei.domain.po.Article;
import com.awei.domain.po.Category;
import com.awei.domain.po.User;
import com.awei.service.ICategoryService;
import com.awei.utils.Md5Util;
import com.awei.utils.ThreadLocalUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author awei
 * @since 2024-11-15
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;

    /**
     * 新增文章分类
     */
    @PostMapping
    public Result addArticleCategory(String categoryName, String categoryAlias){

        Category category = new Category();
        category.setCategoryName(categoryName);
        category.setCategoryAlias(categoryAlias);
        Integer id = ThreadLocalUtil.get();
        category.setCreateUser(id);
        //更新数据到数据库
        categoryService.save(category);
        return Result.success();
    }


    /**
     * 文章分类列表
     * @return
     */
    @GetMapping
    public Result<List<Category>> categoryList(){
        QueryWrapper<Category> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.last("limit 10");
        return Result.success( categoryService.list(userQueryWrapper));
    }
    /**
     * 文章分类列表
     * @return
     */
    @GetMapping("/detali")
    public Result<Category> categoryDetails(Integer queryString){
        //根据queryString查询分类
        Category category = categoryService.getById(queryString);
        return Result.success(category);
    }

    /**
     * 修改文章分类
     * @return
     */
    @PutMapping()
    public Result updateCategory(@RequestBody Map<String,String> map) {
        String categoryName = map.get("categoryName");
        String categoryAlias = map.get("categoryAlias");
        Integer id = Integer.parseInt(map.get("id"));
        Category category = new Category();
        category.setId(id);
        category.setCategoryName(categoryName);
        category.setCategoryAlias(categoryAlias);
        categoryService.updateById(category);
        return Result.success();
    }

    /**
     * 删除文章
     * @param queryString
     * @return
     */
    @DeleteMapping
    public Result deleteCategory(Integer queryString){
        boolean b = categoryService.removeById(queryString);
        if (!b) {
            return Result.error("删除失败");
        }
        return Result.success();
    }
}
